package com.wenyu.wenyuyuedu.fragment.homepagefragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.PullableListView;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.homepage.MyAttentionActivity;
import com.wenyu.wenyuyuedu.adapter.homepagefragment.DeitryAttentionAdapter;
import com.wenyu.wenyuyuedu.bean.homepager.DeityAttentionBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.homepager.Home_DeityAttention_Server;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * 神贴
 */
public class DeityAttentionFragment extends Fragment {


    @InjectView(R.id.iv_defult)
    ImageView ivDefult;
    @InjectView(R.id.deityattention_content_view)
    PullableListView deityattentionContentView;
    @InjectView(R.id.deityattention_refresh_view)
    PullToRefreshLayout deityattentionRefreshView;
    private View deityFragmentView;
    private String token;
    private int page = 1;
    private List<DeityAttentionBean.InfoBean> infoAll = new ArrayList<>();
    private ListView pullableView;
    private List<DeityAttentionBean.InfoBean> info;
    private boolean isLoading = false;
    private DeitryAttentionAdapter adapter;

    public DeityAttentionFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        deityFragmentView = inflater.inflate(R.layout.fragment_deity_attention, container, false);
        ButterKnife.inject(this, deityFragmentView);
        adapter = new DeitryAttentionAdapter(getActivity());
        deityattentionContentView.setAdapter(adapter);
        initPutoRefresh();
        return deityFragmentView;
    }
    @Override
    public void onStart() {
        super.onStart();
        isLoading = false;
        request();
    }
    /*
    * 初始化下拉刷新
    * */
    private void initPutoRefresh() {
        pullableView = (ListView) deityattentionRefreshView.getPullableView();
        deityattentionRefreshView.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                new Handler() {
                    public void handleMessage(Message msg) {
                        page = 1;
                        isLoading = false;
                        request();
                        //千万别忘了告诉控件刷新完毕了
                        deityattentionRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                new Handler() {
                    public void handleMessage(Message msg) {
                        page++;
                        isLoading = true;
                        request();
                        //千万别忘了告诉控件刷新完毕了
                        deityattentionRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 1000);
            }
        });
        deityattentionContentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), MyAttentionActivity.class);
                DeityAttentionBean.InfoBean infoBean = infoAll.get(i);
                String id = infoBean.getId();
                String title = infoBean.getTitle();
                String nickname = infoBean.getNickname();
                intent.putExtra("allattentionnickname", nickname);
                intent.putExtra("allattentionid", id);
                intent.putExtra("allattentiontitle", title);
                startActivity(intent);
            }
        });
    }

    /*
        * 初始化网络数据
        * */
    private void request() {
        token = AbSharedUtil.getString(getActivity(), SharedConstants.USER_TOKEN);
        Home_DeityAttention_Server home_deityAttention_server = ApiWrapper.getInstance().create(Home_DeityAttention_Server.class);
        Call<DeityAttentionBean> deity = home_deityAttention_server.getDeity(token, page, 3);
        deity.enqueue(new Callback<DeityAttentionBean>() {
            @Override
            public void onResponse(Call<DeityAttentionBean> call, Response<DeityAttentionBean> response) {
                DeityAttentionBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        if (body.getInfo() != null) {
                            info = body.getInfo();
                            if(info.size() != 0){
                                if (!isLoading) {
                                    infoAll.clear();
                                }
                                infoAll.addAll(info);
                                adapter.addAll(infoAll);
                                ivDefult.setVisibility(View.GONE);
                            }else {
                                ivDefult.setVisibility(View.VISIBLE);
                            }

                        }else {
                            ivDefult.setVisibility(View.VISIBLE);
                        }
                    }else if(code == 2101){
                        ivDefult.setVisibility(View.VISIBLE);
                    }
                } else {
                    ivDefult.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<DeityAttentionBean> call, Throwable t) {
                ivDefult.setVisibility(View.VISIBLE);
                MyToast.showToast(getActivity(), "网络异常", Toast.LENGTH_SHORT);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}